package com.orangelife.mobile.surrounding.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.net.NetworkUtil;
import com.curry.android.view.stickylistheaders.StickyListHeadersAdapter;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.main.biz.JumpUtil;
import com.orangelife.mobile.surrounding.activity.HotGoodsDetailActivity;
import com.orangelife.mobile.util.BaseViewHolder;
import com.thirdsdk.qiniu.QiniuHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 新式商品列表适配器
 * Created by ChenXiang on 2016/4/5.Modify by 绯若虚无 on 160419.
 *
 * @author ChenXiang
 * @since v3.6.0
 */
public class GoodListRightAdapter<T> extends FatherAdapter<T> implements
        StickyListHeadersAdapter, SectionIndexer {

    private Context mContext;
    private List<T> mlist = new ArrayList<>();
    private DisplayImageOptions options;

    public GoodListRightAdapter(Context context, List<T> list) {
        this.mContext = context;
        this.mlist = list;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.shop_header)
                .showImageForEmptyUri(R.drawable.shop_header)
                .showImageOnFail(R.drawable.shop_header)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(10))
                .build();
    }

    @Override
    public BaseAdapter setList(List<T> list) {
        this.mlist = list;
        return GoodListRightAdapter.this;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_goods, null);
        }
        ImageView ivPhoto = BaseViewHolder.get(convertView, R.id.ivPhoto);
        TextView tvGoodsName = BaseViewHolder.get(convertView, R.id.tvGoodsName);
        TextView tvStandard = BaseViewHolder.get(convertView, R.id.tvStandard);
        TextView tvPrice = BaseViewHolder.get(convertView, R.id.tvPrice);
        TextView tvOriPrice = BaseViewHolder.get(convertView, R.id.tvOriPrice);
        RelativeLayout rlGoods = BaseViewHolder.get(convertView, R.id.rl_goods);
        Map<String, Object> map = (Map<String, Object>) mlist.get(position);

        tvGoodsName.setText(String.valueOf(map.get("goodsName")));
        tvStandard.setText(String.valueOf(map.get("pdtDesc")));
        tvPrice.setText(Constant.RMB_FLAG + String.valueOf(map.get("price")));
        tvOriPrice.setText(Constant.RMB_FLAG + String.valueOf(map.get("mkPrice")));
        tvOriPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        ImageLoader.getInstance().displayImage(QiniuHelper.getUrlWithQiniuImageSize(String.valueOf(map.get("goodsLogo")), QiniuHelper.QiniuImageSize.IMAGE_SIZE_SMALL_PNG), ivPhoto,options);
        rlGoods.setOnClickListener(new listener(map));

        return convertView;

    }

    @Override
    public Object[] getSections() {
        return new Object[0];
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return 0;
    }

    @Override
    public int getSectionForPosition(int position) {
        return 0;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_header, null);
        }
        TextView tv = BaseViewHolder.get(convertView, R.id.text1);
        Map<String,String> map=(Map<String,String>)mlist.get(position);
        tv.setText(map.get("catName"));
        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        return ((Map<String,Object>)mlist.get(position)).get("key").toString().charAt(0);
    }

    class listener implements View.OnClickListener {

        private Map<String, Object> clikmap = null;

        public listener(Map<String, Object> map) {

            this.clikmap = map;
        }

        @Override
        public void onClick(View view) {

            if (!NetworkUtil.isConnectedNet(mContext)) {
                Toast.makeText(
                        mContext,
                        mContext.getResources().getString(
                                R.string.net_disconnected),
                        Toast.LENGTH_SHORT).show();
                return;
            }
            if(("").equals(String.valueOf(clikmap.get("modelUrl")))||"null".equals(String.valueOf(clikmap.get("modelUrl")))) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("detail", (Serializable) clikmap);
                intent.setClass(mContext, HotGoodsDetailActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }else{
                JumpUtil.jumpActivity(mContext, clikmap.get("modelUrl") + "", true);
            }

        }

    }
}
